#include "system.h"

extern uint16_t adc_ordinary_valuetab;

struct flapp_tmr cur_tmr_buf;


static void flapp_read_err_ion()
{
	uint32_t adc_voltage;
	static uint8_t ion_err_cnt = FALSE;
	adc_voltage = (adc_ordinary_valuetab * 3300) >> 12;	//本来是>>12，但是这里电压取的1/2，所以直接>>11
	if((adc_voltage < ERR_ION_LOW) || (adc_voltage > ERR_ION_HIGH))	
	{
		ion_err_cnt++;
		if(ion_err_cnt > ERR_ION_COUNT)		//连续50次离子故障上报离子故障
		{
			ion_err_cnt = ERR_ION_COUNT;	
			flapp_set_master_err(TRUE,DEVICE_ERROR_ION);
		}
	}
	else
	{
		ion_err_cnt = FALSE;
		flapp_set_master_err(FALSE,DEVICE_ERROR_ION);
	}
}

static void flapp_read_err_fan()
{
	static uint8_t fan_err_cnt = FALSE;
	cur_tmr_buf.fan_speed = cur_tmr_buf.fan_freq * 30;		//转换每分钟多少转
	
	if((cur_tmr_buf.fan_speed < ERR_FAN_LOW) || (cur_tmr_buf.fan_speed > ERR_FAN_HIGH))
	{
		fan_err_cnt++;
		if(fan_err_cnt > ERR_FAN_COUNT)		//连续50次风扇故障上报离子故障
		{
			fan_err_cnt = ERR_FAN_COUNT;	
			flapp_set_master_err(TRUE,DEVICE_ERROR_FAN);
		}
	}
	else
	{
		fan_err_cnt = FALSE;
		flapp_set_master_err(FALSE,DEVICE_ERROR_FAN);
	}
	cur_tmr_buf.fan_freq = FALSE;
}

void flapp_tmr_run(void)
{
	flapp_read_err_ion();
	flapp_read_err_fan();
}


void flapp_set_adc_sta(confirm_state mark)
{
	cur_tmr_buf.flag_adc_trans = mark;
}

void flapp_fan_speed_plus(void)
{
	cur_tmr_buf.fan_freq++;
}

/**********************************************************
函数功能：获取定时器接口结构体
入口参数：无
出口参数：cur_tmr_buf
说	  明 
***********************************************************/ 
struct flapp_tmr flapp_get_tmr_status(void)
{
	return cur_tmr_buf;
}
